﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
using System.Linq;
using WPFExcelReport.Chart;
namespace WPFExcelReport
{

    public class YGRVK02LISTData : ViewModelBase
    {
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string UserID
        {
            get
            {
                return app.User;
            }
        }
        public bool checkVK07 = true;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        private string _DEND; public string DEND { get { return _DEND; } set { _DEND = value; OnPropertyChanged("DEND", value); } }
        public string UserName
        {
            get
            {
                return app.User;
            }
        }
        public string date = "", year = "", month = "", day = "";
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        public ChartExBase Chart1
        {
            get;
            set;


        }
        public ChartExBase Chart2
        {
            get;
            set;
        }
        public ChartExBase Chart3
        {
            get;
            set;
        }
        public YGRVK02LISTData()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }

            Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {

            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_WFEVENTDEF.NewRow();
                dwData["MANDANT"] = "00";
                dwData["WFCLASS"] = "901";
                SResult = INTEXDBC.QueryICC_WFEVENTDEF(dwData, 2);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["WFEDES"] = Helpers.Helper.GetGB2312Value(row["WFEDES"].ToString().Trim());
                }
                SReturn.Merge(SResult);
                /*dwData = INTEXDB.SDDAT.NewRow();
                dwData["SDSA"] = "0013";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0114";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0734";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0241";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0208";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0209";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999)); 
                dwData["SDSA"] = "0477";
                SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));*/
                SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwData, 3));
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public object SetUpChart(object P1 = null, object P2 = null, object P3 = null, object P4 = null, object P5 = null)
        {
            try
            {
                if (Chart1 != null)
                {
                    Chart1.SetChartSleep();
                    DataTable tb = SetMaster.Tables["QVW_VKPOS"];
                    string name = "2013";
                    string X = "KDSNAME";
                    string Y = "VKPQTY";
                    Chart1.RATIO = 1;
                    Chart1.Converter = 0.001;
                    Chart1.SetUp(1, name, tb, X, Y);
                    name = "2014";
                    Chart1.RATIO = 0.3;
                    Chart1.Converter = 0.001;
                    Chart1.SetUp(2, name, tb, X, Y);
                    /*name = "2014";
                    Chart1.RATIO = 0.4;
                    Chart1.Converter = 0.001;
                    Chart1.SetUp(3, name, tb, X, Y);
                    name = "2014";
                    Chart1.RATIO = 0.5;
                    Chart1.Converter = 0.001;
                    Chart1.SetUp(4, name, tb, X, Y);
                    name = "2015";
                    Chart1.RATIO = 0.6;
                    Chart1.Converter = 0.001;
                    Chart1.SetUp(5, name, tb, X, Y);*/
                    Chart1.Confirm();
                    Chart1.SetChartGetUp();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return "转换图表完成";
        }
        public DataSet QueryWFEVENTS(DataRow dwInput)
        {

            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_WFEVENTS.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["WFEVENTS"] = dwInput["WFEVENTS"];
                dwData["CONTROL"] = "NO ";
                dwData["WFEUSER"] = dwInput["WFEUSER"];
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 5);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return SReturn;
        }
        public DataSet BuildData(string client, string KRS, string LFD, string endate = "99999999")
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = client;
                dwData["C1"] = " ";
                dwData["C2"] = "NA";
                dwData["DEND"] = _DEND;
                SResult = INTEXDBC.QueryVKPOS(dwData, 7);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["KDKUNDNA"] = Helper.GetGB2312Value(row["KDKUNDNA"].ToString()).Trim();
                    row["KDNAME1"] = Helper.GetGB2312Value(row["KDNAME1"].ToString()).Trim();
                    row["KDLAND"] = Helper.GetGB2312Value(row["KDLAND"].ToString()).Trim();
                    row["KDPLZ"] = Helper.GetGB2312Value(row["KDPLZ"].ToString()).Trim();
                    row["KDORT"] = Helper.GetGB2312Value(row["KDORT"].ToString()).Trim();
                    row["KDBZ"] = Helper.GetGB2312Value(row["KDBZ"].ToString()).Trim();
                    row["KDSTRASSE1"] = Helper.GetGB2312Value(row["KDSTRASSE1"].ToString()).Trim();

                    row["COLORSTD"] = Helper.GetGB2312Value(row["COLORSTD"].ToString()).Trim();
                    row["VKCOM"] = Helper.GetGB2312Value(row["VKCOM"].ToString()).Trim();
                    row["VKPKDFARBNR"] = Helper.GetGB2312Value(row["VKPKDFARBNR"].ToString()).Trim();
                    row["STRENGTH"] = Helper.GetGB2312Value(row["STRENGTH"].ToString()).Trim();
                    row["TWIST"] = Helper.GetGB2312Value(row["TWIST"].ToString()).Trim();
                    row["VAAPRGR"] = Helper.GetGB2312Value(row["VAAPRGR"].ToString()).Trim();
                }
                var query = from XD in SResult.Tables[0].AsEnumerable()
                            group XD by new
                            {
                                VKPLIEFTERM = XD["VKPLIEFTERM"].ToString(),
                                VKKKUNDNR = XD["VKKKUNDNR"].ToString(),
                            }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    VKPMENGE1 = g.Sum(XD => Convert.ToDecimal(XD["VKPMENGE1"].ToString())),
                                    QTYPLAN = g.Sum(XD => Convert.ToDecimal(XD["QTYPLAN"].ToString())),
                                };
                foreach (var GW in query)
                {
                    string VKPLIEFTERM = (string)GW.KEY.VKPLIEFTERM;
                    string VKKKUNDNR = (string)GW.KEY.VKKKUNDNR;
                    var qVKP = from DataRow row in SResult.Tables[0].AsEnumerable()
                               where row["VKPLIEFTERM"].ToString() == VKPLIEFTERM
                               && row["VKKKUNDNR"].ToString() == VKKKUNDNR
                               select row;
                    foreach (DataRow row in qVKP)
                    {
                        row["VKKMENGE1"] = (decimal)GW.VKPMENGE1;
                    }
                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public DataSet QueryVKAN(string client, string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = client;
                dwData["C1"] = " ";
                dwData["C2"] = "AN";
                SResult = INTEXDBC.QueryVKPOS(dwData, 7);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["KDKUNDNA"] = Helper.GetGB2312Value(row["KDKUNDNA"].ToString()).Trim();
                    row["VKCOM"] = Helper.GetGB2312Value(row["VKCOM"].ToString()).Trim();
                    row["VKPKDFARBNR"] = Helper.GetGB2312Value(row["VKPKDFARBNR"].ToString()).Trim();
                    row["STRENGTH"] = Helper.GetGB2312Value(row["STRENGTH"].ToString()).Trim();
                    row["VKPZUSATZ"] = Helper.GetGB2312Value(row["VKPZUSATZ"].ToString()).Trim();
                    row["TWIST"] = Helper.GetGB2312Value(row["TWIST"].ToString()).Trim();
                    row["VAAPRGR"] = Helper.GetGB2312Value(row["VAAPRGR"].ToString()).Trim();
                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public DataSet QueryVR02(string client, string KRS, string LFD, string edate = "99999999")
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VVTMANDANT"] = client;
                dwData["DEND"] = edate;
                SResult = INTEXDBC.QueryVVLIEF(dwData, 4);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["KDKUNDNA"] = Helper.GetGB2312Value(row["KDKUNDNA"].ToString()).Trim();
                    row["VKPKDARTNR"] = Helper.GetGB2312Value(row["VKPKDARTNR"].ToString()).Trim();
                    row["VKPKDFARBNR"] = Helper.GetGB2312Value(row["VKPKDFARBNR"].ToString()).Trim();
                }
                SResult.Tables[0].TableName = "VKPOS";
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }

        public DataSet QueryGBDaily(string client, string sdate = "99999999")
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = client;
                dwData["C2"] = "01";
                dwData["C3"] = "CP";
                dwData["DSTART"] = sdate;
                dwData["C20"] = "'13','10','43','20'";
                SResult = INTEXDBC.QueryGABEW(dwData, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {

                }
                SResult.Tables[0].TableName = "VKPOS";
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public DataSet QueryOTD(string client, string KRS, string LFD, bool ChkFinish, string _year, string _month)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = client;
                dwData["C1"] = "J";
                dwData["DSTART"] = _year + _month;
                SResult = INTEXDBC.QueryVKPOS(dwData, 8);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["KDKUNDNA"] = Helper.GetGB2312Value(row["KDKUNDNA"].ToString()).Trim();
                    row["VKCOM"] = Helper.GetGB2312Value(row["VKCOM"].ToString()).Trim();
                    row["VKPKDFARBNR"] = Helper.GetGB2312Value(row["VKPKDFARBNR"].ToString()).Trim();
                    row["STRENGTH"] = Helper.GetGB2312Value(row["STRENGTH"].ToString()).Trim();
                    row["VKPZUSATZ"] = Helper.GetGB2312Value(row["VKPZUSATZ"].ToString()).Trim();
                    row["TWIST"] = Helper.GetGB2312Value(row["TWIST"].ToString()).Trim();
                    row["VAAPRGR"] = Helper.GetGB2312Value(row["VAAPRGR"].ToString()).Trim();
                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}